package com.ltt.controller;

import com.alibaba.fastjson.JSONObject;
import com.ltt.annotation.NoAuth;
import com.ltt.common.UserThreadLocal;
import com.ltt.dao.pojo.Chat;
import com.ltt.dao.pojo.FileItems;
import com.ltt.dao.pojo.User;
import com.ltt.dao.vo.Result;
import com.ltt.mapper.ChatMapper;
import com.ltt.mapper.FileMapper;
import com.ltt.mapper.UserInfoMapper;
import com.ltt.service.BaiduAuthService;
import com.ltt.service.FileService;
import com.ltt.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author ‘李天泰’
 * @version 1.0
 * @description: TODO
 * @date 2023/5/1 19:23
 */
@CrossOrigin(origins = "*")
@Api(tags = "文件模块")
@RestController
@RequestMapping("/file")
@Slf4j
public class FileController {
    @Autowired
    private BaiduAuthService baiduAuthService;

    @Autowired
    private FileService fileService;

    @Autowired
    private FileMapper fileMapper;

    @Autowired
    private ChatMapper chatMapper;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserInfoMapper userInfoMapper;


    @ApiOperation("上传文件")
    @PostMapping("/upload")
    public Result uploadPic(@RequestBody MultipartFile file) throws IOException {
        String str = fileService.upload(file.getBytes(),file.getOriginalFilename());
        log.info("用户openid:{},上传图片url:{}", UserThreadLocal.get().getOpenid(),str);
//        System.out.println(files);
//        files.setFileName(file.getOriginalFilename());
//        files.setFileUrl(str);
//        files.setUserId(UserThreadLocal.get().getOpenid());
        return Result.successWithData(str);
    }

    @ApiOperation("创建资源")
    @PostMapping("/insert")
    public Result insertResource(@RequestBody FileItems file){
        System.out.println(file);
        file.setUserId(UserThreadLocal.get().getOpenid());
        int res = fileMapper.insertFile(file);
        int id = fileMapper.selectLast();
        System.out.println(id);
        Chat chat = new Chat();
        chat.setContent(file.getContent());
        chat.setInitiator(UserThreadLocal.get().getOpenid());
        chat.setGroupId(file.getGroupId());
        chat.setFileId(id);
        int row = chatMapper.setChat(chat);
        User user = new User();
        user.setOpenid(UserThreadLocal.get().getOpenid());
        userInfoMapper.updateIntegral(user);
        return row > 0 && res > 0 ? Result.successWithoutData() : Result.failed("服务器繁忙");
    }


    @ApiOperation("创建资源")
    @PostMapping("/insertOnly")
    public Result insertResourceOnly(@RequestBody FileItems file){
        System.out.println(file);
        file.setUserId(UserThreadLocal.get().getOpenid());
        int res = fileMapper.insertFile(file);
        User user = new User();
        user.setOpenid(UserThreadLocal.get().getOpenid());
        userInfoMapper.updateIntegral(user);
        return res > 0 ? Result.successWithoutData() : Result.failed("服务器繁忙");
    }
    @ApiOperation("查询文件")
    @PostMapping("/find")
    @NoAuth
    public Result find(FileItems files) throws IOException {
        List<FileItems> filess = fileMapper.findById(files);
        for(FileItems file: filess) {
            User user = userInfoService.findUserById(file.getUserId());
            file.setUser(user);
        }
        return Result.successWithData(filess);
    }

    @ApiOperation("删除文件")
    @PostMapping("/delete")
    @NoAuth
    public Result delete(FileItems files) throws IOException {
        fileMapper.deleteFile(files);
        return Result.successWithoutData();
    }




    @ApiOperation("识别图片")
    @PostMapping("/distinguish")
    public Result searchPic(String url) {
        if(url == null || url == ""){
            return null;
        }
        String token = baiduAuthService.getAuth();
        JSONObject plant = fileService.plant(token, url);
        log.info("用户openid:{},识别图片信息:{}",UserThreadLocal.get().getOpenid(),plant.toJSONString());
        return Result.successWithData(plant);
    }
}
